package com.dmz.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.echronos.commons.Result;
import com.echronos.commons.annotation.Permission;
import com.dmz.workflow.constant.PermissionConstants;
import com.dmz.workflow.entity.FormGroup;
import com.dmz.workflow.service.FormGroupService;
import com.dmz.workflow.vo.FormGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程分组管理
 **/
@Slf4j
@RestController
@RequestMapping("/v1/work/group")
public class ProcessGroupController {

    @Autowired
    private FormGroupService groupingFormService;

    /**
     * 分组列表展示
     **/
    @GetMapping(value = "/list")
    public Result<IPage<FormGroup>> queryList(FormGroupVo echGroupFormVO) {
        IPage<FormGroup> formPage = groupingFormService.queryListById(echGroupFormVO);
        return Result.build(formPage);
    }

    /**
     * 拖拽分组排序
     * 需要 所有分组 ID 和sort
     **/
    @PutMapping(value = "/updateById")
    @Permission(PermissionConstants.PROCESS_SETTING_ADVANCE)
    public Result<?> updateGroup(@RequestBody List<FormGroupVo> echGroupFormVO) {
        groupingFormService.update(echGroupFormVO);
        return Result.build();
    }

    /**
     * 根据id删除分组
     **/
    @DeleteMapping(value = "/deleteById")
    @Permission(PermissionConstants.PROCESS_SETTING_ADVANCE)
    public Result<?> deleteGroup(Integer id) {
        groupingFormService.deleteGroup(id);
        return Result.build();
    }

    /**
     * 新增分组
     **/
    @PostMapping(value = "/save")
    @Permission(PermissionConstants.PROCESS_SETTING_ADVANCE)
    public Result<?> addGroup(@RequestBody FormGroupVo echGroupFormVO) {
        return groupingFormService.addFormGroup(echGroupFormVO);
    }

    /**
     * 修改分组名称
     */
    @PostMapping(value = "/updateGroupName")
    @Permission(PermissionConstants.PROCESS_SETTING_ADVANCE)
    public Result<?> updateGroupName(@RequestBody FormGroupVo groupFormVO) {
        return groupingFormService.updateGroupName(groupFormVO);
    }

    /**
     * 根据id查询进行回显
     **/
    @GetMapping("/echo")
    public Result<FormGroupVo> queryGroup(Integer id) {
        return Result.build(groupingFormService.queryGroup(id));
    }


}
